using System;
using System.Collections.Generic;
using System.Text;
using csammisrun.shaim;

namespace DefaultUI
{
  public delegate void MenuUpdatedHandler(ShaimMenuLocation location);
  public delegate void ToolbarLocationUpdatedHandler(ShaimButtonLocation location);

  partial class DefaultUISet
  {
    private Dictionary<ShaimButtonLocation, List<ShaimToolbarButton>> _buttons =
      new Dictionary<ShaimButtonLocation, List<ShaimToolbarButton>>();
    private Dictionary<ShaimMenuLocation, List<ShaimMenuItem>> _menus =
      new Dictionary<ShaimMenuLocation, List<ShaimMenuItem>>();

    public event MenuUpdatedHandler MenuUpdated;
    public event ToolbarLocationUpdatedHandler ToolbarLocationUpdated;

    /// <summary>
    /// Adds a <see cref="ShaimMenuItem"/> to the UI
    /// </summary>
    public void AddMenuItem(ShaimMenuItem item)
    {
      ShaimMenuLocation location = item.Parent;
      if (!_menus.ContainsKey(location))
      {
        _menus.Add(location, new List<ShaimMenuItem>());
      }

      _menus[location].Add(item);

      if (this.MenuUpdated != null)
      {
        this.MenuUpdated(location);
      }
    }

    /// <summary>
    /// Removes a <see cref="ShaimMenuItem"/> from the UI
    /// </summary>
    public void RemoveMenuItem(ShaimMenuItem item)
    {
      ShaimMenuLocation location = item.Parent;
      if (_menus.ContainsKey(location))
      {
        _menus[location].Remove(item);

        if (this.MenuUpdated != null)
        {
          this.MenuUpdated(location);
        }
      }
    }

    public List<ShaimMenuItem> GetMenuList(ShaimMenuLocation location)
    {
      if (_menus.ContainsKey(location))
      {
        return _menus[location];
      }
      return new List<ShaimMenuItem>();
    }

    /// <summary>
    /// Adds a <see cref="ShaimToolbarButton"/> to the UI
    /// </summary>
    public void AddToolbarButton(ShaimToolbarButton item)
    {
      ShaimButtonLocation location = item.Location;
      if (!_buttons.ContainsKey(location))
      {
        _buttons.Add(location, new List<ShaimToolbarButton>());
      }

      if (!_buttons[location].Contains(item))
      {
        _buttons[location].Add(item);

        if (this.ToolbarLocationUpdated != null)
        {
          this.ToolbarLocationUpdated(location);
        }
      }
    }

    /// <summary>
    /// Removes a <see cref="ShaimToolbarButton"/> from the UI
    /// </summary>
    public void RemoveToolbarButton(ShaimToolbarButton item)
    {
      ShaimButtonLocation location = item.Location;
      if (_buttons.ContainsKey(location))
      {
        if (_buttons[location].Contains(item))
        {
          _buttons[location].Remove(item);

          if (this.ToolbarLocationUpdated != null)
          {
            this.ToolbarLocationUpdated(location);
          }
        }
      }
    }

    public List<ShaimToolbarButton> GetButtonList(ShaimButtonLocation location)
    {
      if (_buttons.ContainsKey(location))
      {
        return _buttons[location];
      }
      return new List<ShaimToolbarButton>();
    }
  }
}
